package me.alex.dialogdismissdemo;

import android.graphics.Rect;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    RelativeLayout dialog;
    TextView close;
    ImageView icon;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        dialog = findViewById(R.id.main_dialog);
        icon = findViewById(R.id.main_icon);
        close = findViewById(R.id.main_close);

        close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Rect dialogRect = getRect(dialog);
                Rect iconRect = getRect(icon);

//                float scaleX = iconRect.width() * 1.0f / dialogRect.width();
//                float scaleY = iconRect.height() * 1.0f / dialogRect.height();
                float scaleX = 0.1f;
                float scaleY = 0.1f;

                System.out.println("scaleX = " + scaleX);
                System.out.println("scaleY = " + scaleY);

                float pivotX = (iconRect.right - dialogRect.left - dialogRect.width() * scaleX) / (1 - scaleX);
                float pivotY = (iconRect.bottom - dialogRect.top - dialogRect.height() * scaleY) / (1 - scaleY);
//                float pivotX = iconRect.right - (icon.getWidth() / 2);
//                float pivotY = iconRect.bottom - (icon.getHeight() / 2);
                System.out.println("pivotX = " + pivotX);
                System.out.println("pivotY = " + pivotY);

                Animation animation = new ScaleAnimation(1, scaleX, 1, scaleY, pivotX, pivotY);
                animation.setDuration(1000);
                animation.setFillAfter(true);

                dialog.startAnimation(animation);
                animation.setAnimationListener(new Animation.AnimationListener() {
                    @Override
                    public void onAnimationStart(Animation animation) {

                    }

                    @Override
                    public void onAnimationRepeat(Animation animation) {

                    }

                    @Override
                    public void onAnimationEnd(Animation animation) {
//                        dialog.clearAnimation();
//                        dialog.setVisibility(View.INVISIBLE);
                    }
                });
            }
        });
        showDia();
    }

    void showDia() {
        dialog.setVisibility(View.VISIBLE);
    }

    public Rect getRect(View view) {
        int[] result = new int[2];
        view.getLocationInWindow(result);
        return new Rect(result[0], result[1], result[0] + view.getWidth(), result[1] + view.getHeight());
    }
}